package editor.weapon;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import entity.weapons.Weapon;

public class UI extends JFrame implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 
	 */
	
	private JMenuBar menu;
	private JMenu file;
	
	private JMenuItem newWeapon;
	private JMenuItem saveWeapon;
	private JMenuItem loadWeapon;
	private JMenuItem exit;
	
	private WeaponDialog dialog;
	
	private Weapon current;
	
	private View panel;

	public UI()
	{
		this.setSize( 500, 500 );
		
		menu = new JMenuBar();
		
		file = new JMenu( "File" );
		menu.add( file );
		
		newWeapon = new JMenuItem( "New Weapon" );
		saveWeapon = new JMenuItem( "Save Weapon" );
		loadWeapon = new JMenuItem( "Load Weapon" );
		exit = new JMenuItem( "Exit" );
		file.add( newWeapon );
		file.add( saveWeapon );
		file.add( loadWeapon );
		file.add( exit );
		
		panel = new View();
		panel.setPreferredSize( new Dimension( 32 * 10, 32 * 10 ) );
		
		newWeapon.addActionListener( this );
		saveWeapon.addActionListener( this );
		loadWeapon.addActionListener( this );
		exit.addActionListener( this );
		
		this.setJMenuBar( menu );
		
		this.add( panel );
		this.pack();
		
		current = null;
		
		this.setDefaultCloseOperation( EXIT_ON_CLOSE );

		this.setVisible( true );
	}

	public void actionPerformed( ActionEvent e ) 
	{
		Object o = e.getSource();
		if( o.equals( newWeapon ) )
		{
			WeaponDialog wd = new WeaponDialog( this );
		
			if( wd.getWeapon() != null )
				current = wd.getWeapon();
		
			panel.repaint();
		}
		else if( o.equals( saveWeapon ) )
		{
			if( current != null )
				current.save();
		}
		else if( o.equals( loadWeapon ) )
		{
			Weapon w = Weapon.load( this );
			if( w != null )
			{
				current = w;
				
				panel.repaint();
			}
		}
		else if( o.equals( exit ) )
		{
			System.exit( 0 );
		}
	}
	
	public static void main( String[] args )
	{
		new UI();
	}
	
	private class View extends JPanel implements MouseListener, MouseMotionListener
	{
		private int x, y;
		
		public View()
		{
			this.addMouseListener( this );
			this.addMouseMotionListener( this );
		}
		
		public void paintComponent( Graphics g )
		{
			g.setColor( Color.white );
			g.fillRect( 0, 0, this.getWidth(), this.getHeight() );
			
			if( current != null )
			{
				g.translate( this.getWidth() / 2 , this.getHeight() / 2 );
				current.draw( (Graphics2D) g );
				g.translate( -this.getWidth() / 2 , -this.getHeight() / 2 );
			}
		}

		public void mouseClicked( MouseEvent e ) 
		{
		}

		public void mouseEntered( MouseEvent e ) {}

		public void mouseExited( MouseEvent e ) {}
		
		public void mousePressed( MouseEvent e ) {}

		public void mouseReleased( MouseEvent e ) {}

		public void mouseDragged( MouseEvent e ) 
		{
		}

		public void mouseMoved( MouseEvent e ) 
		{
		}
	}
}
